package User;

import Constant.constant;
import book.Book;
import book.Library;
import book.PairOfUserIDAndBookID;
import utils.AnalyzingBorrowedBook;

import java.io.IOException;
import java.util.Scanner;

public class NormalUser extends User{
    private int borrowedCount;//借阅次数
    private PairOfUserIDAndBookID[] pairOfUserIDAndBookIDs;
    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();
    Scanner scanner = new Scanner(System.in);
    public NormalUser(int ID, String name) {
        super(ID, name, "普通用户");
        loadBorrowedBook();
    }
    private void storeUserBook() {
        try {
            analyzingBorrowedBook.storeUserBook(pairOfUserIDAndBookIDs,constant.BORROW_BOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void loadBorrowedBook() {
        try {
            //加载读取文件内容
            PairOfUserIDAndBookID[] allBorrowedBook = analyzingBorrowedBook.loadUserBook(constant.BORROW_BOOK_FILE_NAME);
            //TODO:可以让每一个人最多借阅5本，而不是所有人加起来最多借阅5本（这时候需要传参判断类型，进行for循环遍历++）
            //默认总共借阅容量为5，其类型是 借阅书籍类PairOfUserIDAndBookID
            pairOfUserIDAndBookIDs = new PairOfUserIDAndBookID[constant.BORROW_BOOK_MAX_NUM];
            //判断是否含有数据
            if(allBorrowedBook == null) {
                borrowedCount =0;
            }else {
                //获得实际有多少书籍,判断是否超出容量
                if(pairOfUserIDAndBookIDs.length < allBorrowedBook.length) {
                    pairOfUserIDAndBookIDs = new PairOfUserIDAndBookID[allBorrowedBook.length];//按照情况重新分配数组内存
                }
                //存放入借阅信息对象pairOfUserIDAndBookID中
                for (int i = 0; i < allBorrowedBook.length; i++) {
                    pairOfUserIDAndBookIDs[i] = allBorrowedBook[i];
                }
                borrowedCount = allBorrowedBook.length;//借阅次数为文件中的数组个数
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int getID() {
        return super.getID();
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getRole() {
        return super.getRole();
    }

    @Override
    int mune() {
        System.out.println("普通⽤⼾ " + getName() + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        Library.getInstance().displayBook();
        scanner.nextLine();
        System.out.println("请输入你要借阅书籍ID：");
        int bookBorrowedID = scanner.nextInt();
        if(Library.getInstance().getBookCount() == 0) {
            System.out.println("目前图书馆中没有书籍，不能借阅！");
            return;
        }
        //加载借阅信息
        loadBorrowedBook();
        Book book = Library.getInstance().searchBookID(bookBorrowedID);
        if(book == null) {
            System.out.println("没有找到ID为："+bookBorrowedID+"的图书：");
            return;
        }
        //对这个书籍是否被借阅进行判断
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUserIDAndBookID pairOfUserIDAndBookID1 = pairOfUserIDAndBookIDs[i];
            if(pairOfUserIDAndBookID1.getBookID() == book.getNumber()) {//借阅的书籍ID == 要借阅的书籍ID
                if(pairOfUserIDAndBookID1.getUserID() == getID()) {//借阅人的ID == 这个用户的ID
                    System.out.println("这本书已经被你借阅过了，你的ID是："+getID());
                    book.decreaseBorrowCount();
                    return;
                }else {
                    System.out.println("这本书已经被其他人借阅过了！");
                    return;
                }
            }
        }

        Library.getInstance().borrowBook(bookBorrowedID);
        //将信息写入allborrowbooks.txt文件中
        PairOfUserIDAndBookID pairOfUserIDAndBookID = new PairOfUserIDAndBookID(getID(),book.getNumber());
        pairOfUserIDAndBookIDs[borrowedCount] = pairOfUserIDAndBookID;
        borrowedCount++;
        storeUserBook();
        System.out.println("ID为："+book.getNumber()+"，书籍"+book.getTitle()+"借阅成功！");
    }
    //归还图书
    public void returnBook() {
        loadBorrowedBook();
        if(borrowedCount == 0) {
            System.out.println("没有人借阅书籍");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入你要归还的书籍ID：");
        int returnBookBorrowedID = scanner.nextInt();
        scanner.nextLine();
        Book book = Library.getInstance().searchBookID(returnBookBorrowedID);
        if(book == null) {
            System.out.println("没有该ID为："+returnBookBorrowedID+"的图书：");
            return;
        }
        //对这个书籍是否被借阅进行判断
        for (int i = 0; i < borrowedCount; i++) {
            PairOfUserIDAndBookID pairOfUserIDAndBookID = pairOfUserIDAndBookIDs[i];
            if(pairOfUserIDAndBookID.getBookID() == returnBookBorrowedID) {//借阅的书籍ID == 归还书籍ID
                if(pairOfUserIDAndBookID.getUserID() == getID()) {//借阅人的ID == 这个用户的ID
                    Library.getInstance().returnBook(returnBookBorrowedID);
                    System.out.println("ID为："+book.getNumber()+"，书籍"+book.getTitle()+"归还成功！");
                    //由于归还图书，将allborrowbooks.txt文件的借阅信息删除
                    //用数组PairOfUserIDAndBookID[]中最后的元素将归还信息覆盖，将最后的元素置为null
                    pairOfUserIDAndBookIDs[i] = pairOfUserIDAndBookIDs[borrowedCount-1];
                    pairOfUserIDAndBookIDs[borrowedCount - 1] = null;
                    borrowedCount--;
                    storeUserBook();
                    return;
                }else {
                    System.out.println("这本书不是你借阅的，你不能归还："+book.getTitle());
                }
            }
        }
        System.out.println("这本书没有被借阅过，不需要归还");
    }
    //查看个人借阅情况
    public void viewBorrowBook() {
        loadBorrowedBook();
        System.out.println("你的借阅情况如下：");
        if(borrowedCount == 0) {
            System.out.println("目前没有借阅信息...");
        }else {
            boolean flag = false;
            for (int i = 0; i < borrowedCount; i++) {
                if(pairOfUserIDAndBookIDs[i].getUserID() == getID()) {
                    flag = true;
                    Book book = Library.getInstance().searchBookID(pairOfUserIDAndBookIDs[i].getBookID());
                    System.out.println(book);
                }
            }
            if(!flag) {
                System.out.println("你没有借阅过书籍...");
            }
        }
    }

}
